﻿using DataAccess;
using Model;
using Model.CustomDataType;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BusinessLogic
{
    public class TreatmentBL: MarshalByRefObject
    {
        private TreatmentDA treatmentDA;
        public override object InitializeLifetimeService()
        {
            return null;
        }
        public TreatmentBL()
        {
            treatmentDA = new TreatmentDA();
        }

        public Treatment createTreatment()
        {
            return new Treatment();
        }
        public TreatmentDetail createTreatmentDetail()
        {
            return new TreatmentDetail();
        }

        public int addTreatment(Treatment treatment)
        {
            return treatmentDA.addTreatment(treatment);
        }

        public int getTreatmentCost()
        {
            return treatmentDA.getTreatmentCost();
        }

        public bool updateTreatment(Treatment treatment)
        {
            if (treatment == null)
                return false;
            int status = getTreatmentStatus(treatment.getTreatmentId());
            if (status == 3)//đã khám rồi, ko cho sửa khám nữa
                return false;
            return treatmentDA.updateTreatment(treatment);
        }
        
        private int getTreatmentStatus(int treId)
        {
            return treatmentDA.getCurrentStatusId(treId);
        }

        public bool updateTreatmentStatusId(int treatmentId, int newStatusId)
        {
            int oldStatus = treatmentDA.getCurrentStatusId(treatmentId);
            if (oldStatus == -1 || newStatusId <= oldStatus)
            {
                return false;
            }
            else
            {
                return treatmentDA.updateTreatmentStatusId(treatmentId, newStatusId);
            }
        }
        /// <summary>
        /// Update pay status. 1 means treated, 0 means not treated
        /// </summary>
        /// <param name="treatmentId"></param>
        /// <param name="newStatusId"></param>
        /// <returns></returns>
        public bool updatePayStatusId(int treatmentId, short newStatusId)
        {
            short oldStatus = treatmentDA.getCurrentPayStatusId(treatmentId);
            if (newStatusId ==0 ||oldStatus == 1)//Treatmented
            {
                return false;
            }
            else
            {
                return treatmentDA.updateTreatmentStatusId(treatmentId, newStatusId);
            }
        }
        public Treatment getTreatmentById(int treatmentID)
        {
            return treatmentDA.getTreatmentById(treatmentID);
        }

        public Treatment[] getAllTreatment()
        {
            return treatmentDA.getAllTreatment();
        }

        public Treatment[] getTreatmentByDateOfTreament(CMDateTime date)
        {
            return treatmentDA.getTreatmentByDateOfTreament(date);
        }

        public Treatment[] getTreatmentByRawQuery(string query)
        {
            return treatmentDA.getTreatmentByRawQuery(query);
        }

        ////////////////////////////////////////////////////
        //
        // Treatment status
        //
        ////////////////////////////////////////////////////
        /// <summary>
        /// Add a new treatment status. Current values: 0: Waiting, 1: Getting treatment, 2: Got Treatment
        /// </summary>
        /// <param name="name"></param>
        /// <returns></
        /// returns>
        public bool addTreatmentStatus(int value)
        {
            return treatmentDA.addTreatmentStatus(value);
        }

        ////////////////////////////////////////////////////
        //
        // Treatment Advice
        //
        ////////////////////////////////////////////////////

        public bool addAdvice(string name)
        {
            return treatmentDA.addAdvice(name);
        }

        public bool updateAdvice(int id, string name)
        {
            return treatmentDA.updateAdvice(id, name);
        }

        ////////////////////////////////////////////////////
        //
        // Treatment Symptom
        //
        ////////////////////////////////////////////////////

        public bool addSymptom(string name)
        {
            return treatmentDA.addSymptom(name);
        }

        public bool updateSymptom(int id, string name)
        {
            return treatmentDA.updateSymptom(id, name);
        }

        ////////////////////////////////////////////////////
        //
        // Treatment diagnosis
        //
        ////////////////////////////////////////////////////

        public bool addDiagnosis(string name)
        {
            return treatmentDA.addDiagnosis(name);
        }

        public bool updateDiagnosis(int id, string name)
        {
            return treatmentDA.updateDiagnosis(id, name);
        }

        public string[] getAllDiagnosis()
        {
            return treatmentDA.getAllDiagnosis();
        }

        public int getDiagnosisIdByName(string name)
        {
            return treatmentDA.getDiagnosisIdByName(name);
        }
        ////////////////////////////////////////////////////
        //
        // Treatment detail
        //
        ////////////////////////////////////////////////////

        public bool addTreatmentDetail(TreatmentDetail treatmentDe)
        {
            return treatmentDA.addTreatmentDetail(treatmentDe);
        }

        public bool updateTreatmentDetail(TreatmentDetail treatmentDe)
        {
            return updateTreatmentDetail(treatmentDe);
        }
        public TreatmentDetail getTreatmentDetailById(int treatmentDeID)
        {
            return treatmentDA.getTreatmentDetailById(treatmentDeID);
        }

        public TreatmentDetail[] getAllTreatmentDetail()
        {
            return treatmentDA.getAllTreatmentDetail();
        }


        public TreatmentDetail[] getTreatmentDetailByRawQuery(string query)
        {
            return treatmentDA.getTreatmentDetailByRawQuery(query);
        }

    }
}
